define(["jquery"], function () {
    'use strict';
    // 业务代码
    let iptPhoneNumContainer = $(".phone");
    let iptPhoneNum = $("#ipt-phonenum");
    let tipPhoneNum = $("#tip-phone");
    let phoneRegin = $("#phoneRegin");
    let sliderContainer = $("#ct-slider");
    let slider = $("#slider");
    let sliderTip = $(".slider-tip");
    let chkReadProtacal = $("#chk-readProtacal");
    let btnRegister = $("#btn-submit");
    let wSliderContainer = sliderContainer.width();
    let wSlider = slider.width();
    let phoneNumValidated = false;
    let sliderValidated = true;
    let yzcodeValidated = false;
    let isReadPro = chkReadProtacal.attr("checked");
    let doc = $(document);
    let yzcontainer = $(".yzcodeContainer");
    let btnSendYzcode = $("#btn-yzcode");
    let iptYzcode = $("#ipt-yzcode");
    let code = 0;
    let tipYzcode = $("#tip-yzcode");
    let iptPwd = $("#ipt-pwd");
    let tipPwd = $("#tip-pwd");
    let iptPwd_repeat = $("#ipt-pwd-repeat");
    let tipPwd_repeat = $("#tip-pwd-repeat");
    let pwdValidated = false;
    let pwdConfirmValidated = false;
    //验证电话号码
    function validatePhoneNum() {
        let reg = /^1[^210]\d{9}$/;
        if (iptPhoneNum.val().match(reg) == null) {
            phoneNumMsg(false);
            phoneNumValidated = false;
            return false;
        }
        phoneNumMsg(true);
        phoneNumValidated = true;
        validate();
    }
    //验证滑块
    function validateSlider() {
        if (!phoneNumValidated) {
            phoneNumMsg();
        }
        var sliderPath = parseInt(slider.css("left"));
        var tWidth = sliderContainer.width() - slider.width();
        if (sliderPath >= tWidth) {
            sliderContainer.css({
                background: "#78C13F",
                color: "white"
            });
            sliderTip.text("通过验证");
            slider.off("mousedown");
            sliderValidated = true;
            validate();
        }
    }
    //添加事件
    function addEvent() {
        iptPhoneNum.on("focusout", validatePhoneNum);
        // slider.on("mousedown", onSliderDown);
        // doc.on("mouseup", $.proxy(onSliderUp, slider));//slider
        phoneRegin.on("click", getPhoneRegin);
        chkReadProtacal.on("click", validateReadProtacal);
        btnSendYzcode.on("click", sendYzcode);
        iptYzcode.on("focusout", validateYzcode);
        iptPwd.on("focusout", validatePwd);
        iptPwd_repeat.on("focusout", confirmPwd);
        btnRegister.on("click", goMain);
    }
    var x = null;
    //当滑块被按下
    function onSliderDown(evt) {
        let e = evt || window.event;
        x = e.offsetX;
        doc.off("mousemove");
        doc.on("mousemove", $.proxy(onSliderMove, sliderContainer));
    }
    //滑块移动
    function onSliderMove(evt) {
        let e = evt || window.event;
        let toX = e.clientX - x - this.offset().left;
        if (toX >= this.width() - slider.width()) {
            toX = this.width() - slider.width();
        }
        slider.css({
            left: toX + "px",
        })
    }
    function onSliderUp() {
        doc.off("mousemove");
        if (this.offset().left - sliderContainer.offset().left < sliderContainer.width() - this.width()) {
            this.animate({
                left: 0,
            }, 500);
        }
        validateSlider();
    }
    //电话号码错误提示
    function phoneNumMsg(isRight) {
        iptPhoneNumContainer.css({
            border: `${isRight ? "1px solid #ddd" : "1px solid red"}`,
        });
        tipPhoneNum.html("请输入正确的电话号码!");
        tipPhoneNum.css({
            visibility: `${isRight ? "collapse" : "visible"}`,
            padding: `${isRight ? "5px 0" : "10px 0"}`,
        });
    }
    //获取电话号码所属区域
    function getPhoneRegin() {
        tipPhoneNum.html("正在开发中...");
        tipPhoneNum.css({
            visibility: "visible",
        });
    }
    //验证是否阅读协议
    function validateReadProtacal(evt) {
        let e = evt || window.event;
        isReadPro = e.target.checked;
        if (isReadPro) {
            chkReadProtacal.css({
                background: "#ffe300",
            })
        }
        validate();
    }
    //设置注册按钮样式
    function setBtnStyle(isRight) {
        let res = isRight ? "false" : "true";
        btnRegister.attr("disabled", !res);//
        btnRegister.css({
            // background: `${isRight ? "#78C13F" : "#eee"}`,
            cursor: `${isRight ? "pointer" : "auto"}`,
            "box-shadow": `${isRight ? "1px 1px #6AA739" : "none"}`,
            color: `${isRight ? "white" : "#bbbbbb"}`
        })
    }
    //发送验证码
    function sendYzcode() {
        code = "1234";
        btnSendYzcode.attr({
            "disabled": true
        })
        btnSendYzcode.val("已发送")
        setTimeout(() => {
            btnSendYzcode.attr({
                "disabled": false
            })
            btnSendYzcode.val("发送验证码")
        }, 60000);
    }
    //验证验证码
    function validateYzcode() {
        yzcodeValidated = iptYzcode.val() == code ? true : false;
        setYzcodeStyle(iptYzcode.val() == code);
        validate();
    }
    //设置验证码输入框样式
    function setYzcodeStyle(isRight) {
        iptYzcode.css({
            background: `${isRight ? "transparent" : "#FCF5F4"}`,
            "border-color": `${isRight ? "#ddd" : "red"}`,
        })
        tipYzcode.css({
            visibility: `${isRight ? "collapse" : "visible"}`,
            padding: `${isRight ? "5px 0" : "10px 0"}`,
        });
        // if (!isRight) iptYzcode[0].focus();
    }
    //验证密码
    function validatePwd() {
        let val = iptPwd.val();
        let reg = /^[a-z\u2E80-\u9FFF_0-9]{6,20}$/i;
        let res = val.match(reg);
        console.log(res, 111111);
        pwdValidated = !res ? false : true;
        setPwdStyle(pwdValidated);
        validate();
    }
    function confirmPwd() {
        let val = iptPwd.val();
        let valConfirm = iptPwd_repeat.val();
        console.log(val, valConfirm);
        pwdConfirmValidated = val === valConfirm ? true : false;
        setConfirmPwdStyle(pwdConfirmValidated);
        validate();
    }
    //设置确认密码框样式
    function setConfirmPwdStyle(isRight) {
        iptPwd_repeat.css({
            background: `${isRight ? "transparent" : "#FCF5F4"}`,
            "border-color": `${isRight ? "#ddd" : "red"}`,
        })
        tipPwd_repeat.css({
            visibility: `${isRight ? "collapse" : "visible"}`,
            padding: `${isRight ? "5px 0" : "10px 0"}`,
        });
        // if (!isRight) iptPwd[0].focus();
    }
    //设置密码框样式
    function setPwdStyle(isRight) {
        iptPwd.css({
            background: `${isRight ? "transparent" : "#FCF5F4"}`,
            "border-color": `${isRight ? "#ddd" : "red"}`,
        })
        tipPwd.css({
            visibility: `${isRight ? "collapse" : "visible"}`,
            padding: `${isRight ? "5px 0" : "10px 0"}`,
        });
        // if (!isRight) iptPwd[0].focus();
    }
    //验证通过
    function validate() {
        // console.log(phoneNumValidated, sliderValidated, isReadPro);
        // if (phoneNumValidated && sliderValidated) {
        //     yzcontainer.css({
        //         display: "block",
        //     })
        // }
        // if (phoneNumValidated && sliderValidated && yzcodeValidated && pwdValidated && isReadPro) {
        //     setBtnStyle(true);
        // } else {
        //     setBtnStyle(false);
        // }
        return phoneNumValidated && sliderValidated && yzcodeValidated && pwdValidated && isReadPro;
    }
    function register() {
        var url = "http://zhaoyx0907.com/users/register.do";
        $.ajax(
            {
                url: url,
                data: {
                    username: $(iptPhoneNum).val(),
                    password: $(iptPwd).val(),
                },
                type: "post"
            }
        ).then(function (res) {
            if(res==0){
                alert("注册成功");
                location.href = "./login.html";
            }else if(res==1){
                alert("注册失败，用户已存在！！！");
            }

        }, function (error) {
            alert("注册失败");
            console.log(error)
        })
    }
    function goMain() {
        var res = validate();
        if (!res) return false;
        register();
        // location.href = "./login.html";
    }
    //初始化
    function init() {
        addEvent();
    }
    init();
})